import { IpcRendererEvent } from 'electron';
import { useDispatch } from 'react-redux';
import { Dispatch } from 'redux';
import React from 'react';
import MLog from '../../util/main-log';
import { ProgressInfo } from '../../../main/biz/ipc/protocol/progress';
import {
  COMMON_UPDATE_FEATURE_PROGRESS,
  ProgressAction,
} from '../state-manager/action-type.const';

const TAG = 'AntiCorruptionLayer';

function AntiCorruptionLayer(props: React.PropsWithChildren) {
  const { children } = props;
  const dispatch = useDispatch<Dispatch<ProgressAction>>();
  window.electronAPI.ipcRenderer.receiveProgress(
    (event: IpcRendererEvent, progressInfo: ProgressInfo) => {
      dispatch({
        type: COMMON_UPDATE_FEATURE_PROGRESS,
        progressInfo,
      });
    }
  );
  return <>{children}</>;
}

export default AntiCorruptionLayer;
